﻿using System;
using System.Collections.ObjectModel;
using HandsonSoft.Web.WebFormsModelBinder;

namespace SampleWeb
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Retrieve employee details from the database.
                var employee = GetEmployeeFromDb();

                // Bind the model to page controls.
                ModelBinder.BindControl(employee, this);
            }
        }

        protected void SubmitClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                // Bind the control values to model.
                var employee = ModelBinder.BindModel<Employee>(this);

                // Do something like:
                // EmployeeService.UpdateEmployee(employee);
            }
        }

        private static Employee GetEmployeeFromDb()
        {
            return new Employee
                       {
                           EmployeeId = 1,
                           FirstName = "Handy",
                           LastName = "Torres",
                           EmployeeType = "CON",
                           Gender = "M",
                           DateOfBirth = new DateTime(1975, 03, 23),
                           NoOfDependants = 2,
                           IsActive = true,
                           Skills = new Collection<int> { 0, 1, 2 },
                           Hobbies = new Collection<int> { 0, 2 },
                           Address = new Address
                                         {
                                             Street = "St 55", 
                                             City = "Pasir Ris", 
                                             StateOrProvince = "", 
                                             CountryCode = "SG", 
                                             PostalCode = "510585"
                                         }
                       };
        }
    }
}
